<template>
  <div>
    <tableComponents
      :isShow="false"
      @toAdd="add_data"
      :isHaveExport="false"
      :isHavePagination="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list"
          stripe
          v-loading="loading"
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
          :default-sort="{ prop: 'sort' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input
                @change="sortNew(scope.$index, scope.row)"
                v-model.number="scope.row.sort"
                autocomplete="off"
                size="small"
              ></el-input>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="100">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="等级" min-width="100">
            <template slot-scope="scope">{{ scope.row.rating_cn }}</template>
          </el-table-column>

          <el-table-column label="价格" min-width="100">
            <template slot-scope="scope">{{
              $fnc.toFixedZ(scope.row.price)
            }}</template>
          </el-table-column>

          <el-table-column label="市场价" min-width="100">
            <template slot-scope="scope">{{
              $fnc.toFixedZ(scope.row.market_price)
            }}</template>
          </el-table-column>

          <el-table-column label="成本价" min-width="100">
            <template slot-scope="scope">{{
              $fnc.toFixedZ(scope.row.cost_price)
            }}</template>
          </el-table-column>

          <el-table-column prop="name" label="是否开启" min-width="100">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.show"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="已开启"
                inactive-text="已关闭"
                active-value="1"
                @change="getIs($event, scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="添加时间" min-width="100">
            <template slot-scope="scope">
              <div class="user_table">
                {{ scope.row.created_time | getTimeFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button
                  :loading="$store.state.isLoading"
                  size="mini"
                  @click="editbtn(scope.row)"
                  type="primary"
                  >编辑</el-button
                >
                <el-button
                  :loading="$store.state.isLoading"
                  size="mini"
                  type="danger"
                  @click="deletebtn(scope.row.id)"
                  >删除</el-button
                >
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>
    <el-dialog
      :close-on-click-modal="false"
      title="编辑"
      class="tl size_13"
      width="800px"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="live_single" label-width="120px" ref="form">
        <el-form-item label="标题：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              placeholder="请输入"
              v-model="live_single.title"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图片：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              disabled
              v-model="live_single.piclink"
            ></el-input>
            <uploadpic
              :piclink="live_single.piclink"
              :width="250"
              :height="150"
              :real_width="150"
              :real_height="100"
              :is_more="false"
              @send_pic="onSuccess"
            ></uploadpic>
          </el-col>
        </el-form-item>
        <el-form-item label="等级：">
          <el-col :span="16">
            <el-select
              v-model="live_single.rating"
              clearable
              placeholder="请选择等级"
            >
              <el-option
                v-for="item in rattinglist"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="分类：">
          <el-col :span="16">
            <el-select
              v-model="live_single.cid"
              clearable
              placeholder="请选择等级"
            >
              <el-option
                v-for="item in vip_cate_lists"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="价格：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              placeholder="请输入"
              v-model="live_single.price"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="市场价：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              placeholder="请输入"
              v-model="live_single.market_price"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="成本价：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              placeholder="请输入"
              v-model="live_single.cost_price"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="限购数量：">
          <el-col :span="16">
            <el-input
              type="number"
              clearable
              placeholder="请输入"
              v-model="live_single.restricted"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="权益:">
          <el-col :span="4">
            <el-button
              :loading="$store.state.isLoading"
              size="mini"
              type="primary"
              @click="add_explain"
            >
              添加
            </el-button>
          </el-col>
          <el-col :span="24" v-for="(item, i) in live_single.explain" :key="i">
            <el-col :span="4" :offset="1">
              标题：<el-input
                v-model="item.tit"
                size="small"
                style="width: 100px"
              >
              </el-input>
            </el-col>
            <el-col :span="4" :offset="1">
              副标题：<el-input
                v-model="item.desc"
                size="small"
                style="width: 110px"
              >
              </el-input>
            </el-col>
            <el-col :span="4" :offset="1">
              图标：
              <uploadpic
                :piclink="item.pic"
                :width="80"
                :height="80"
                :is_more="false"
                :is_tip="false"
                @send_pic="onSuccess_pic($event, i)"
              ></uploadpic>
            </el-col>

            <el-col :span="4" :offset="1">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="del_packet(i)"
              >
                删除
              </el-button>
            </el-col>
          </el-col>
        </el-form-item>

        <el-form-item label="赠送商品：">
          <el-col :span="4">
            <el-button
              :loading="$store.state.isLoading"
              size="mini"
              type="primary"
              @click="xzshop('xzShop')"
            >
              添加商品
            </el-button>
          </el-col>
          <el-col :span="24" v-for="(item, i) in live_single.give_cn" :key="i">
            <span v-if="item[0] && item[0].id">
              {{ item[0].title }}&nbsp;&nbsp;&nbsp;单价：{{ item[0].price }}元
            </span>
            <br />
            数量：
            <el-input v-model="item.number" size="small" style="width: 100px">
            </el-input>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <el-button
              :loading="$store.state.isLoading"
              size="mini"
              type="danger"
              @click="del_packet(i)"
            >
              删除
            </el-button>
          </el-col>
        </el-form-item>

        <el-form-item label="赠送优惠券：">
          <el-col :span="4">
            <el-button
              :loading="$store.state.isLoading"
              size="mini"
              type="primary"
              @click="xzshop('xz_packet')"
            >
              添加优惠券
            </el-button>
          </el-col>
          <el-col :span="24" v-for="(item, i) in live_single.coupon" :key="i">
            {{ item.title }}&nbsp;&nbsp;&nbsp;数量：
            <el-input v-model="item.num" size="small" style="width: 100px">
            </el-input>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <el-button
              :loading="$store.state.isLoading"
              size="mini"
              type="danger"
              @click="del_packet1(i)"
            >
              删除
            </el-button>
          </el-col>
        </el-form-item>

        <el-form-item label-width="120px">
          <el-button
            :loading="$store.state.isLoading"
            type="primary"
            @click="editlive(live_single)"
            >编 辑</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <keep-alive>
      <component
        :is="current"
        :show="show_xz"
        :isAll="false"
        :isAllSearch="false"
        showType="list"
        @sendShop="getSend"
        @isshow="getShow"
      ></component>
    </keep-alive>
  </div>
</template>
<script>
import xz_packet from "@/components/currency/xz_packet.vue";
import xzShop from "@/components/currency/xz_shop";
import uploadpic from "@/components/page/pic/up_pic";
export default {
  data() {
    return {
      current: "",
      show_xz: false,
      rattinglist: [],
      is_edit: false,
      dialogFormVisible: false,
      live_single: {},
      current: "",
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      row: {},
      sort: "",
      multipleSelection: [],
      showCity: true,
      vip_cate_lists: [],
    };
  },
  components: {
    uploadpic,
    xzShop,
    xz_packet,
  },
  methods: {
    onSuccess_pic(pic, i) {
      this.live_single.explain[i].pic = pic;
    },
    add_explain() {
      this.live_single.explain.push({
        pic: "",
        tit: "",
        desc: "",
      });
    },
    xzshop(str) {
      this.current = str;
      this.show_xz = true;
    },
    getShow(val) {
      this.show_xz = val;
    },
    del_packet(i) {
      this.live_single.give_cn.splice(i, 1);
    },
    del_packet1(i) {
      this.live_single.coupon.splice(i, 1);
    },
    getSend(row) {
      if (this.current == "xz_packet") {
        this.live_single.coupon.push({
          id: row.id,
          title: row.title,
          num: 1,
        });
      } else {
        var obj = {
          0: {
            id: row.id,
            title: row.title,
            piclink: row.piclink,
            price: row.price,
          },
          number: 1,
        };
        this.live_single.give_cn.push(obj);
      }
    },
    onSuccess(pic) {
      this.live_single.piclink = pic;
    },
    sorta(a, b) {
      return b - a;
    },
    sortNew(index, row) {
      var params = {};
      params = row;
      params.id = row.id;
      params.sort = row.sort;
      this.$api.getShops.edit_upgrade_data(params).then((res) => {
        if (res.data.code == 200 && res.data.result) {
          return;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteAll() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思",
        }).then((willDelete) => {
          if (willDelete.value) {
            this.$api.getShops.del_upgrade_data({ id_str: str }).then((res) => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", res.data.result, "success");
                this.getNewsAll(this.searchAll);
              } else {
                this.$swal("删除失败！", res.data.result, "success");
                this.getNewsAll(this.searchAll);
              }
            });
          }
        });
      }
    },
    deletebtn(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getShops.del_upgrade_data({ id: id }).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", res.data.result, "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", res.data.result, "success");
              this.getNewsAll(this.searchAll);
            }
          });
        }
      });
    },
    add_data() {
      this.live_single = {
        title: "",
        piclink: "",
        price: "0",
        give_cn: [],
        coupon: [],
        explain: [],
      };
      this.is_edit = false;
      this.dialogFormVisible = true;
    },

    getIs(val, row) {
      this.is_edit = true;
      var params = {};
      params.id = row.id;
      params.show = val;
      params.title = row.title;
      params.rating = row.rating;
      params.price = row.price;
      params.market_price = row.market_price;
      params.cost_price = row.cost_price;
      params.cid = row.cid;
      params.day = row.day;
      this.$api.getShops.edit_upgrade_data(params).then((res) => {
        if (res.data.code == 200) {
          this.dialogFormVisible = false;
          this.$fnc.alertSuccess(res.data.result);
          this.getNewsAll(this.searchAll);
        } else {
          this.$fnc.alertError(res.data.result);
        }
      });
    },

    editlive() {
      var params = JSON.parse(JSON.stringify(this.live_single));

      var give = [];
      if (this.live_single.give_cn) {
        for (let i of this.live_single.give_cn) {
          if (i[0] && i[0].id && i.number > 0) {
            give.push({
              id: i[0].id,
              number: i.number,
            });
          }
        }
      }
      params.give = JSON.stringify(give);
      params.give_cn = JSON.stringify(this.live_single.give_cn);
      params.coupon = JSON.stringify(this.live_single.coupon);
      params.explain = JSON.stringify(this.live_single.explain);

      if (this.is_edit == false) {
        params.show = 1;
        this.$api.getShops.add_upgrade_data(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      } else {
        this.$api.getShops.edit_upgrade_data(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    },
    editbtn(item) {
      this.is_edit = true;
      this.live_single = JSON.parse(JSON.stringify(item));
      if (this.live_single.coupon) {
        this.live_single.coupon = JSON.parse(this.live_single.coupon);
      } else {
        this.live_single.coupon = [];
      }
      if (this.live_single.explain) {
        this.live_single.explain = JSON.parse(this.live_single.explain);
      } else {
        this.live_single.explain = [];
      }
      if (!this.live_single.give_cn) {
        this.live_single.give_cn = [];
      }
      this.dialogFormVisible = true;
    },
    receive_search(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getShops.get_upgrade_data(params).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
    reload() {
      this.getNewsAll(this.searchAll);
    },
    ratting() {
      this.$api.getUser.getRatingList({}).then((res) => {
        if (res.data.code == 200) {
          this.rattinglist = res.data.result.splice(1);
          this.getNewsAll();
        }
      });

      this.$api.getStation.vip_system_cate_lists({}).then((res) => {
        if (res.data.code == 200) {
          this.vip_cate_lists = res.data.result;
        }
      });
    },
  },
  beforeCreate() {},
  created() {
    this.ratting();
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
